package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.service.UserInfoService;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.BeansUtil.RequestBeanUtil;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.action.MD5;
import com.zany.util.action.SessionInfo;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JSONArray;
import com.zany.util.json.JSONException;
import com.zany.util.json.JSONObject;
import com.zany.util.json.JsonTools;


public class UserInfoAction extends AbstractActionSupport{

	private static final long serialVersionUID = -8361556725381628859L;
	private static Log logger = LogFactory.getLog(UserInfoAction.class);
	private List<UserInfoVO> userInfoList = new ArrayList<UserInfoVO>();
	private List<UserInfoVO> roleList = new ArrayList<UserInfoVO>();
	private UserInfoVO userInfoVO = new UserInfoVO();
	private UserInfoService userInfoService;
	
	public void getList() throws ZanyException, JSONException{
		if(logger.isInfoEnabled()){logger.info("UserInfoAction.getList()");}
		response = ServletActionContext.getResponse();
		userInfoVO.setUserName(request.getParameter("userName"));
		userInfoVO.setUserOrg(request.getParameter("orgId"));
		userInfoVO.setUserCode(request.getParameter("userCode"));
		String flag = request.getParameter("flag");
		Integer pageTotal = userInfoService.getAllUserInfoCount(userInfoVO);
		if(flag!=null){
			userInfoVO = userInfoService.getUserInfoDetail(userInfoVO);
			userInfoList.add(userInfoVO);
			AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(userInfoList) + "}", response);
		}else{
			String start = request.getParameter("start");
			String pageStart = (start== null || start.equals("")?"1":start);
			userInfoVO.setPageMessage(Integer.valueOf(pageStart));
			userInfoList = userInfoService.getAllUserInfoPage(userInfoVO);
			AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(userInfoList) + "}", response);
		}
	}
	
	public void updateSts() throws ZanyException {
		if(logger.isInfoEnabled()){logger.info("UserInfoAction.updateSts()");}
		response = ServletActionContext.getResponse();
		String id[] = request.getParameterValues("updateId");
		userInfoVO.setSts(request.getParameter("sts"));
		try{
			if(id.length>0){
				for (int i = 0; i < id.length; i++){
					userInfoVO.setUserCode(id[i]);
					userInfoService.updateUserInfo(userInfoVO);
				}
				AjaxTools.exAjax("{success:true}", response);
			}
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	
	public void update() throws Exception{
		if(logger.isInfoEnabled()){logger.info("PasswordAdminAction.changePassword()");}
		response = ServletActionContext.getResponse();
		String id[] = request.getParameterValues("updateId");
		try{
			userInfoService.updatePassword(id);
			AjaxTools.exAjax("{success:true}", response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	
	public void insert() throws Exception {
		if(logger.isInfoEnabled()){logger.info("UserInfoAction.insert()");}
		response = ServletActionContext.getResponse();
		userInfoVO = (UserInfoVO)RequestBeanUtil.getReqToBean(UserInfoVO.class,request);
		String flag = request.getParameter("flag");
		String userCodeTemp = request.getParameter("userCodeTemp");
		try{
			if(flag.equals("update")){
				userInfoVO.setUserCode(userCodeTemp);
				userInfoService.setUserRole(userInfoVO);
				userInfoService.setUserOrg(userInfoVO);
			}else{
				Integer userNumber = userInfoService.getUserInfo(userInfoVO);
				if(userNumber!=0){
					AjaxTools.exAjax("{\"success\":false,msg:\"用户已存在！\"}",response);
				}else{
					userInfoVO.setCreater(SessionInfo.getUserCode(request));
					userInfoVO.setPassword(MD5.md5("999999"));				
					userInfoService.insertUserInfo(userInfoVO);
					AjaxTools.exAjax("{success:true}", response);					
				}
			}
		}catch(Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		
	}

	public void getRole() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("UserInfoAction.roleValue()");}
		response = ServletActionContext.getResponse();
		roleList = userInfoService.getRoleList(userInfoVO);
		JSONArray listJson = new JSONArray();
		try{
			for(UserInfoVO vo:roleList){
				JSONObject json = new JSONObject();
				json.put("key",vo.getRoleCode());
				json.put("value",vo.getRoleName());
				listJson.put(json);
			}
		}catch(JSONException e){
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		String json = "{param:"+listJson.toString()+"}";
		AjaxTools.exAjax(json,response);
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
}
